package server;

import java.io.PrintWriter;
import java.util.ArrayList;

import artifacts.*;
import test.Main;
import maze.*;

public class Game {
	
	public Maze maze = null;
	boolean stopProgram = false;
	ArrayList<Player> players = new ArrayList<Player>();
	
	public Game() {
		this.maze = createMaze();
	}
	
	public Player createNewPlayer() {
		Player player = new Player();
		player.Enter(this.maze);
		this.players.add(player);
		Artifact.addArtsToMaze(this.maze);
		return player;
	}
	
	public void endThisPart() {
		if(!this.stopProgram) startNewPart();
	}
	
	public void startNewPart() {
		try {
			Main.server = new Communications();
			Main.server.setConnection(this);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	String processCommand(String fromClient, Player pl, PrintWriter netOut) {
		String response = "";
		String[] command = fromClient.split(" ");
		if("LOOK".equalsIgnoreCase(fromClient)) {
			response = look(pl);
		}
		else if("NORTH".equalsIgnoreCase(fromClient) ||
				"SOUTH".equalsIgnoreCase(fromClient) ||
				"EAST".equalsIgnoreCase(fromClient)  ||
				"WEST".equalsIgnoreCase(fromClient)) {
			String direction = Character.toString(fromClient.charAt(0));
			response = move(pl, direction, fromClient, netOut);
		}
		else if("HOME".equalsIgnoreCase(fromClient)) {
			pl.currentRoom = this.maze.entry;
			response = "You are magically transported to the entry of the " + this.maze.name;
		}
		else if("TAKE".equalsIgnoreCase(command[0])) {
			String item = "";
			for(int j = 1; j < command.length; j++) {
				if(item == "") item += command[j];
				else item = item + " " + command[j];
			}
			if("ALL".equalsIgnoreCase(item)) response = takeAllItems(pl);
			else response = takeItem(item, pl);
		}
		else response = "Unknown command";
		return response;
	}
	
	private Maze createMaze() {
		Maze newMaze = new Maze();
		newMaze.createMaze();
		return newMaze;
	}
	
	String look(Player pl) {
		String response = "";
		response = "You find yourself in room " + pl.getRoom().getName() +
		". Exists: " + pl.currentRoom.existDoor();
		if(!pl.currentRoom.items.isEmpty()) {
			String items = roomItems(pl);
			response = response + " " + items + " lie on the floor.";
		}
		String zombie = "";
		for(Player player : this.players) {
			if(player != pl && player.currentRoom == pl.currentRoom && player.getName() != "") {
				if(zombie != "") zombie += ", " + player.getName();
				else zombie += player.getName();
			}
		}
		if(zombie != "") response += " Here rest " + zombie + ".";
		return response;
	}
	
	String move(Player pl, String direction, String fromClient, PrintWriter netOut) {
		String response = "";
		String[] pathList = {direction};
		if(pl.currentRoom.validatePath(pathList)) {
			Room oldRoom = pl.currentRoom;
			pl.Move(direction);
			response = "Leaving through " + fromClient.toLowerCase() + "ern door.";
			if(pl.currentRoom == Maze.out) {
				int points = 0;
				for(Artifact art : pl.inventory) {
					points += art.getCost();
				}
				pl.reiting = points;
				response += " You have reached the exit of the " + this.maze.name +
				" and collected " + Integer.toString(points) + " points. ";
				Player plM = pl;
				for(Player player : this.players) {
					if(plM.reiting < player.reiting && player.foundAnExit) plM = player;
				}
				if(plM != pl) response += plM.getName() + " was better than you with "
					+ Integer.toString(plM.reiting) + " points. Beat that!";
				else response += "Congratulations, you are a winner!";
				pl.foundAnExit = true;
				netOut.println(response);
				Main.server.endConnection();
			}
			else if(pl.currentRoom != oldRoom.getDirection(direction))
				response += " You are teleported into room "	
					+ pl.currentRoom.getName() + ".";
			else 
				response += " You are now in room " 
				+ pl.currentRoom.getName() + ".";
		}
		else response = "You cannot go that way!";
		return response;
	}
	
	String roomItems(Player pl) {
		String items = "";
		items = findItemsAmount(Coin.class, pl);
		if(items != "") {
			if(findItemsAmount(Goblet.class, pl) != "") 
				items = items + ", " + findItemsAmount(Goblet.class, pl);
		}
		else items = findItemsAmount(Goblet.class, pl);
		return items;
	}
	
	String findItemsAmount(Class itemClass, Player pl) {
		int count = 0;
		for(Artifact art : pl.currentRoom.items) {
			if(art.getClass() == itemClass) count++;
		}
		if(count > 1) return Integer.toString(count) + " " 
			+ itemClass.getSimpleName().toLowerCase() + "s";
		else if(count == 1) return "1 " + itemClass.getSimpleName().toLowerCase();
		else return "";
	}
	
	String takeItem(String item, Player pl) {
		String response = "";
		for(Artifact art : pl.currentRoom.items) {
			if(item.equalsIgnoreCase(art.getClass().getSimpleName())) {
				response = "You grab a " +
					art.getClass().getSimpleName().toLowerCase() +
					" and put it in your inventory.";
				pl.inventory.add(art);
				pl.currentRoom.items.remove(art);
				break;
			}
		}
		if(response == "") response = "Cannot find any " + 
			item + "s in this room.";
		return response;
	}
	
	String takeAllItems(Player pl) {
		String response = "";
		if(pl.currentRoom.items.size() > 0) {
			response = "You grab " + roomItems(pl) + " and put it in your inventory.";
			for(Artifact art : pl.currentRoom.items) {
				pl.inventory.add(art);
			}
			pl.currentRoom.items.clear();
		}
		else response = "Nothing to take.";
		return response;
	}
}
